const jwt = require('jsonwebtoken'),
    {
        secret,
        tokenExpires
    } = require('../config');
exports.createToken = user => {
    return jwt.sign(user, secret, {
        expiresIn: tokenExpires
    });
}
exports.vertifyToken = token => {
    try {
        var decoed = jwt.verify(token, secret);
    } catch (e) {

    } finally {
        return decoed;
    }
}
exports.getUser = token => {
    return jwt.decode(token, secret);
}
exports.mkPager = (pageNo, pageSize) => {
    // pageNo当前页:1   pageSize显示多少:5
    // select * from loan limit 0,5
    // 1 => 0  5的零倍   （当前页1 - 1）= 0
    // 2 => 5  5的一倍   （当前页2 - 1）= 1
    // 3 => 10 5的二倍   （当前页3 - 1）= 2
    pageSize = parseInt(pageSize);
    return {
        start: (pageNo - 1) * pageSize,
        end: pageSize
    }
}
exports.checkNullVuale = (obj, checkList) => {
    // checkList.every(field=>{
    //     // 检查obj的字段
    //     if(obj[field] === undefined || obj[field] === null || obj[field] === '')return false;
    //     return true;
    // })

    for (let field of checkList) {
        // 检查obj的字段
        if (obj[field] === undefined || obj[field] === null || obj[field] === '') {
            return {
                result: false,
                field,
                value: obj[field]

            }
        }
    }
    return {
        result: true
    }
}
const fs = require('fs');
exports.fileExist = filePath => {
    try {
        fs.accessSync(filePath)
        return true;
    } catch (e) {
        return false;
    }
}